﻿using Newtonsoft.Json;
using PosClient.Helper;
using PosClient.Interfaces;
using PosClient.Model;
using PosClient.ViewModel;
using Splat;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Concurrency;
using System.Text;
using System.Threading.Tasks;

namespace PosClient.Job.DownLoad
{
    public class BaseDataDownLoadJob:IJob
    {
        public Result<JobEntity> Excute()
        {
            Result<JobEntity> result = new Result<JobEntity>()
            {
                Success = true,
                Message = "BaseData请求成功"
            };
            var viewModel = Locator.Current.GetService<MainViewModel>();
            viewModel._mainScheduler.Schedule(() =>
            {
                viewModel.SubText = "正在下载基础数据...";
                viewModel.Loading = true;
            });
            var JobList = new List<JobEntity>();
            {
                JobEntity jobEntity = new JobEntity()
                {
                    JobName = "BarcodeDownLoadJob",
                    JobBody = "body",
                    JobStatus = 1,
                    Deleted = 0,
                    JobRound = -1,
                    JobNote = "条码数据下载",
                    JobClass = "BarcodeDownLoadJob",
                    JobCron = "0 1/5 * * * ?"
                };
                JobList.Add(jobEntity);
                JobEntity jobEntity1 = new JobEntity()
                {
                    JobName = "BaseDataDownLoadJob",
                    JobBody = "body1",
                    JobStatus = 1,
                    Deleted = 0,
                    JobRound = -1,
                    JobNote = "基础数据下载",
                    JobClass = "BaseDataDownLoadJob",
                    JobCron = "0 1/4 * * * ?"
                };
                JobList.Add(jobEntity1);
                JobEntity jobEntity2 = new JobEntity()
                {
                    JobName = "DiscountDownLoadJob",
                    JobBody = "body2",
                    JobStatus = 1,
                    Deleted = 0,
                    JobRound = -1,
                    JobNote = "削价数据下载",
                    JobClass = "DiscountDownLoadJob",
                    JobCron = "0 1/3 * * * ?"
                };
                JobList.Add(jobEntity2);
                JobEntity jobEntity3 = new JobEntity()
                {
                    JobName = "GoodDownLoadJob",
                    JobBody = "body3",
                    JobStatus = 1,
                    Deleted = 0,
                    JobRound = -1,
                    JobNote = "商品数据下载",
                    JobClass = "GoodDownLoadJob",
                    JobCron = "0 1/2 * * * ?"
                };
                JobList.Add(jobEntity3);
            }
            if (JobList.Count > 0)
            {
                var job = Locator.Current.GetService<JobManager>();
                job.CacheJob(JobList);
            }
            using (var conn = FreeClient.Sqlite)
            {
                //门店数据
                Pos pos = new Pos()
                {
                    StoreCode = "100082",
                    StoreName = "广州百信广场店",
                    PosNo = "0001",
                    Status = 1,
                    BalanceDate = DateTime.Now,
                    Account = "100082"
                };
                conn.InsertOrUpdate<Pos>()
                    .SetSource(pos,it => it.StoreCode)
                    .ExecuteAffrows();
                //用户数据
                User user = new User()
                {
                    Username = "admin",
                    Password = "admin",
                    NickName = "admin",
                    LastLoginTime = DateTime.Now,
                    Status = 1
                };
                conn.InsertOrUpdate<User>()
                    .SetSource(user, it => it.Username)
                    .ExecuteAffrows();
            }
            List<VipLevel> levelList = new List<VipLevel>();
            levelList.Add(new VipLevel() 
            {
                 LevelType = 0,
                 LevelCode = "1",
                 LevelName = "青铜会员",
                 LevelGrade = 0,
                 DisCount = 100m,
                 LevelGradeType = 0
            });
            levelList.Add(new VipLevel()
            {
                LevelType = 1,
                LevelCode = "2",
                LevelName = "白银会员",
                LevelGrade = 25000,
                DisCount = 98m,
                LevelGradeType = 0
            });
            levelList.Add(new VipLevel()
            {
                LevelType = 1,
                LevelCode = "3",
                LevelName = "黄金会员",
                LevelGrade = 50000,
                DisCount = 95m,
                LevelGradeType = 0
            });
            levelList.Add(new VipLevel()
            {
                LevelType = 1,
                LevelCode = "4",
                LevelName = "钻石会员",
                LevelGrade = 70000,
                DisCount = 93m,
                LevelGradeType = 0
            });
            if (levelList.Count > 0)
            {
                var conn = FreeClient.Redis;
                {
                    foreach (var level in levelList.GroupBy(y => y.LevelCode).Select(x => x.FirstOrDefault()).ToDictionary(k => k.LevelCode))
                    {
                        conn.HSet(RedisKey.Redis_VipLevel, level.Key, JsonConvert.SerializeObject(level.Value));
                    }
                }
            }
            List<Keyboard> Keys = new List<Keyboard>();
            Keys.Add(new Keyboard()
            {
                ActionName = "btnSearchMember",
                KeyCode = 87,
                KeyData = "W",
                KeyName = "会员查询",
                Command = "SearchVipAction",
                StoreCode = "100082",
                PosNo = "0001"
            });
            Keys.Add(new Keyboard()
            {
                ActionName = "btnClearCart",
                KeyCode = 80,
                KeyData = "P",
                KeyName = "清空购物车",
                Command = "ClearCartAction",
                StoreCode = "100082",
                PosNo = "0001"
            });
            Keys.Add(new Keyboard()
            {
                ActionName = "btnPresent",
                KeyCode = 70,
                KeyData = "F",
                KeyName = "赠送",
                Command = "PresentActive",
                StoreCode = "100082",
                PosNo = "0001"
            });
            Keys.Add(new Keyboard()
            {
                ActionName = "btnRemove",
                KeyCode = 46,
                KeyData = "Del",
                KeyName = "行情",
                Command = "RemovePluActive",
                StoreCode = "100082",
                PosNo = "0001"
            });
            Keys.Add(new Keyboard()
            {
                ActionName = "btnDown",
                KeyCode = 76,
                KeyData = "L",
                KeyName = "下载数据",
                Command = "DownLoadActive",
                StoreCode = "100082",
                PosNo = "0001"
            });
            Keys.Add(new Keyboard()
            {
                ActionName = "btnChangePrice",
                KeyCode = 90,
                KeyData = "Z",
                KeyName = "改价",
                Command = "ChangePluPriceActive",
                StoreCode = "100082",
                PosNo = "0001"
            });

            if (Keys.Count > 0)
            {
                var conn = FreeClient.Redis;
                {
                    foreach (var Key in Keys.GroupBy(y => y.ActionName).Select(x => x.FirstOrDefault()).ToDictionary(k => k.ActionName))
                    {
                        conn.HSet(RedisKey.Redis_Keyboard, Key.Key, JsonConvert.SerializeObject(Key.Value));
                    }
                }
            }
            viewModel._mainScheduler.Schedule(() =>
            {
                viewModel.SubText = string.Empty;
                viewModel.Loading = false;
            });
            return result;

        }
    }
}
